/* 
    file:   ui_button.c
 */
/* ------------------------------------------------------------------------- */
#include "ui_label.h"
#include "stm32f7_ltdc.h"
#include "ui_draw_img.h"
#include "ui_drawfont.h"
/* ------------------------------------------------------------------------- */

ui_style_t ui_label_def=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0x0000,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        .color = 0x0000,
        .font = &tt_font324,
    }
};
ui_style_t ui_label_24=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0x0000,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        .color = 0xffff,
        .font = &tt_font244,
    }
};
ui_style_t ui_label_32W=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0x0000,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        .color = 0xffff,
        .font = &tt_font324,
    }
};
ui_style_t ui_label_32R=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0x0000,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        .color = 0xc125,
        .font = &tt_font324,
    }
};
ui_style_t ui_label_32B=
{
    .body=
    {
        .main_color = 0xb5b6,
        .grad_color = 0x0000,
        .radius = 0,
        .opa = 0xff,
    },
    .text=
    {
        .color = 0x0000,
        .font = &tt_font324,
    }
};
/* ------------------------------------------------------------------------- */
uint8_t ui_drawstring(struct ui_obj * obj,ui_screen_t * pscr)
{
    point       realpoint;
    uint32_t    fontsize;
    uint8_t *   pstring;
    uint32_t    letter;
    uint32_t    didx;
    lv_point_t  pos_p;
    int sx = 0;
    int sy = 0;

    lv_font_t *pfont;

    ui_getlct(obj,&realpoint);
    sx = obj->area.x2-obj->area.x1;
    sy = obj->area.y2-obj->area.y1;
    pstring = *(uint8_t **)(obj->objdat);

    pos_p.x = realpoint.x;
    pos_p.y = realpoint.y;

    lv_area_t mask_p;
    mask_p.x1 = pos_p.x;
    mask_p.x2 = pos_p.x+sx;
    mask_p.y1 = pos_p.y;
    mask_p.y2 = pos_p.y+sy;

    pfont =  (lv_font_t *)obj->style_p->text.font;
    if(pstring)
    {
        while (*pstring)
        {
            letter = ui_get_letter(pstring,&didx);
            if(letter == 0)
            {
                break;
            }

            ui_draw_letter(&pos_p,&mask_p,obj->style_p->text.font,letter,
                        obj->style_p->text.color, 0xff,pscr);
            if(letter<0x7e)
            {
                pos_p.x += pfont->line_height/2;
            }
            else
            {
                pos_p.x += pfont->line_height;
            }
            pstring = &pstring[didx];
        }
    }
}

uint8_t ui_label_sc(struct ui_obj * obj, uint8_t sign, void * param)
{
    struct ui_core * pcore = NULL;

    if(param)
    {
        pcore = (struct ui_core *)param;
        ui_drawstring(obj,pcore->pscr);
    }
    
    return 0;
}
/* ------------------------------------------------------------------------- */

struct ui_obj * ui_newlabel(struct ui_obj * ppar)
{
    struct ui_obj * pnewobj = NULL;
    pnewobj = ui_newobj(ppar);

    pnewobj->signal_cb = ui_label_sc;

    pnewobj->objdat = tlsf_malloc(sizeof(void *));

    pnewobj->style_p = &ui_label_def;

    *(void **)(pnewobj->objdat) = NULL;

    return pnewobj;
}
void ui_label_set_str(struct ui_obj * pobj,const void * pstr)
{
    *(void **)(pobj->objdat) = (void *)pstr;
    pobj->status = 2;
}
unsigned char getutf8len(unsigned char * pstr)
{
    int i;
    int cnt;
    unsigned char chtemp;
    chtemp = pstr[0];
    cnt = 0;
    for ( i = 0; i < 7; i++)
    {
        if(0x80&chtemp)
        {
            cnt++;
            chtemp = chtemp<<1;
        }
        else
        {
            return cnt;
        }
    }
    return cnt;
}
uint32_t getstrlen(void *pstr)
{
    uint32_t cnt = 0;
    unsigned char * pstrtemp;
    uint32_t i;

    pstrtemp = (unsigned char *) pstr;
    for (size_t i = 0; i < 512; i++)
    {
        if(pstrtemp[cnt])
        {
            cnt++;
        }
        else
        {
            return cnt;
        }
    }
    return i;
}
uint32_t getsrchtlen(void *pstr)
{
    uint32_t cnt = 0;
    int i = 0;
    unsigned char * pstrtemp;
    unsigned char codecnt;
    uint32_t total;

    pstrtemp = (unsigned char *) pstr;
    total = getstrlen(pstr);
    while (1)
    {
        if(pstrtemp[i])
        {
            if(pstrtemp[i]&0x80)
            {
                //utf-8
                codecnt = getutf8len(&pstrtemp[i]);
                if(codecnt)
                {
                    i+=codecnt;
                }
                else
                {
                    i++;
                }
                
                cnt++;//utf-8 字符一般为汉字，直额外加1
            }
            else
            {
                /* code */
                i++;
            }
            cnt++;
            if(i>total)
            {
                return cnt;
            }
        }
        else
        {
            return cnt;
        }
    }
    return cnt;    
}

void ui_label_setpos_midup(struct ui_obj * pobj,uint32_t lx,uint32_t ly)
{
    uint32_t strlen;
    uint32_t xpix;
    lv_font_t * plvfont;
    void * pstr;

    pstr = *(void **)(pobj->objdat);

    strlen = getsrchtlen(pstr);
    plvfont = (lv_font_t *)pobj->style_p->text.font;
    xpix = plvfont->line_height*strlen/2;

    pobj->area.x1 = lx-(xpix/2);
    pobj->area.y1 = ly;
    pobj->area.x2 = pobj->area.x1 + xpix;
    pobj->area.y2 = ly + plvfont->line_height;

}
/* ------------------------------------------------------------------------- */


